package com.kangnam.mongo;

import com.kangnam.mongo.MongoManager;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;


public class InputProduct
{

	private static MongoManager mongoManager = null;
	private static String DB_NAME;
	private static String P_INFO = "product";
	
	public InputProduct(String dbName, String host)
	{
		mongoManager = MongoManager.getInstance();
		mongoManager.createPool(host);
		DB_NAME = dbName;	
	}
	
	public void insertProductInfo(String p_Name, int p_Stock, int p_Price, 			
			String p_Category, String Parent_Category, String Child_Category)
	{
		Mongo mongo = null;
		
		ProduceIdGenerator PIG = new ProduceIdGenerator();
		
		PIG.getInstance();
		
		PIG.getId();
		
		try 
		{
			mongo = mongoManager.getConnection();
			
			DB db = mongo.getDB(DB_NAME);
			
			DBCollection coll = db.getCollection(P_INFO);

			BasicDBObject query = new BasicDBObject();
			query.put("p_ID", PIG.getId());
			query.put("p_Name", p_Name);
			query.put("p_Stock", p_Stock);
			query.put("p_Price", p_Price);
			query.put("p_Sales", 0);
			query.put("p_Category", p_Category);
			query.put("Parent_Category", Parent_Category);
			query.put("Child_Category", Child_Category);
			
			coll.insert(query);
			
		} 
		
		finally 
		{
			mongoManager.close(mongo);
		}
	}
		
	
	public static void viewCollection(String collection) {
		Mongo mongo = null;
		
		try{
			mongo = mongoManager.getConnection();
			System.out.println("viewCollection: " + mongo.hashCode());

			DB db = mongo.getDB(DB_NAME);
			
			DBCollection coll = db.getCollection(collection);
			DBCursor cursor = coll.find();
			while (cursor.hasNext()){
				DBObject dbObject = (DBObject) cursor.next();
				System.out.println(dbObject);
			}
		} finally {
			mongoManager.close(mongo);
		}
	}
	
	public static void main(String[] args) 
	{
		InputProduct InputFT = new InputProduct("db", "127.0.0.1");
		InputFT.insertProductInfo("AMD FX 6300", 100, 124900, "cpu", "computer", "0");
		InputFT.insertProductInfo("AMD FX 6200", 100, 134900, "cpu", "computer", "0");
		InputFT.insertProductInfo("AMD FX 6100", 100, 124900, "cpu", "computer", "0");
		InputFT.insertProductInfo("AMD FX 6900", 50, 124900, "cpu", "computer", "0");
		
		InputFT.viewCollection(P_INFO);
	}
}
